package com.cliff.joinus.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.androidquery.AQuery;
import com.androidquery.callback.BitmapAjaxCallback;
import com.androidquery.util.AQUtility;
import com.cliff.joinus.Constants;
import com.cliff.joinus.Constants.ITEMNODE_TYPE;
import com.cliff.joinus.JoinusControlPoint;
import com.cliff.joinus.LocalMediaDataManager;
import com.cliff.joinus.R;
import com.cliff.joinus.LocalMediaDataManager.FILE_TYPE;
import com.cliff.joinus.LocalMediaDataManager.MEDIA_TYPE;
import com.cliff.joinus.RendererMediator;
import com.cliff.joinus.RendererMediator.MEDIATOR_OPERATION;
import com.cliff.joinus.dataobject.ImageItem;

public class BrowseImageActivity extends SherlockActivity implements OnItemClickListener {
	
	AQuery gridAq = null;

	protected final int[][] ACTIONBARMENU = new int[][]{Constants.AB_BACK, Constants.AB_SELECT_RENDERER};
	ArrayAdapter<ImageItem> mAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_browseimage);		

		setTitle(String.format(getString(R.string.title_activity_browseimage), LocalMediaDataManager.getInstance().getImageList().size()));
		initGridView();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
		MainActivity.initMenu(menu, ACTIONBARMENU);
		
//		return super.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (android.R.id.home == item.getItemId()) {
			finish();
			return true;
		}
		
		if (Constants.GROUP_COMMON == item.getGroupId()) {
			switch(item.getItemId()) {
			case Constants.ITEM_BACK:
				finish();
				break;
			case Constants.ITEM_SELECT_RENDERER:
				break;
			}
		}
//		return super.onOptionsItemSelected(item);
		return true;
	}

	protected void initGridView() {
		AQUtility.cleanCacheAsync(this, 0, 0);
		BitmapAjaxCallback.clearCache();

		gridAq = new AQuery(this);
		
		ArrayList<ImageItem> list = LocalMediaDataManager.getInstance().getImageList();
		
		mAdapter = new ArrayAdapter<ImageItem>(this,
				R.layout.item_grid_image, list) {
			public View getView(int position, View convertView, ViewGroup parent) {
				if (convertView == null) {
					convertView = getLayoutInflater().inflate(
							R.layout.item_grid_image, parent, false);
				}
				ImageItem item = getItem(position);
				String tbUrl = item.filePathName;
				AQuery gridItemAq = gridAq.recycle(convertView);
				Bitmap placeholder = gridItemAq.getCachedImage(R.drawable.image_ph);

				if (gridItemAq.shouldDelay(position, convertView, parent, tbUrl)) {
					gridItemAq.id(R.id.tb).image(placeholder, 1.0f);
				} else {
					gridItemAq.id(R.id.tb).progress(R.id.progress)
					.image(tbUrl, false, true, 100, R.drawable.image_missing, null, AQuery.FADE_IN, 1.0f/1.0f);					
//					.image(tbUrl, imageOptions);
				}
				return convertView;
			}
		};
		gridAq.id(R.id.gridViewImages).adapter(mAdapter);
		gridAq.id(R.id.gridViewImages).itemClicked(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO need to add internalPlay variable fetch
		boolean playExternal = false;
		
		if (null == JoinusControlPoint.getInstance().getSelectedRendererUdn()) {
			//local play
			if (playExternal) {
				ImageItem item = mAdapter.getItem(position);
				LocalMediaDataManager.getInstance().playExternal(item.filePathName, MEDIA_TYPE.IMAGE, FILE_TYPE.LOCAL_FILE);
				return;
			}
			//play use internal player
	        Intent intent = new Intent(BrowseImageActivity.this, PlayImageActivity.class);
	        intent.putExtra(Constants.EXTRA_ITEMNODE_POS, position);        
	        startActivity(intent);
		}
		else {
			//remote play
			
			RendererMediator mediator = JoinusControlPoint.getInstance().getSelectedRendererMediator();
			if (null == mediator) {
				Toast.makeText(this, "Mediator is null, renderer not ready", Toast.LENGTH_SHORT).show();
				return;
			}
			mediator.init(ITEMNODE_TYPE.LOCAL_IMAGE, position, null);
			mediator.operate(MEDIATOR_OPERATION.PLAYPAUSE, position);
			
			Intent intent = new Intent(this, PlayControlActivity.class);
			startActivity(intent);
		}
	}
	
}
